package com.amon.amonplayer.player;

import android.content.Context;
import android.text.TextUtils;
import android.view.Surface;

import java.io.FileDescriptor;

public class AmonPlayer {
    static {
        System.loadLibrary("amon-player");
    }

    private final Context mContext;
    private Surface mSurface;

    public AmonPlayer(Context context) {
        this.mContext = context;
        nInit();
    }

    public String url;
    public FileDescriptor fileDescriptor;

    public void setDataSource(String url) {
        this.url = url;
    }

    public void setDataSource(FileDescriptor fileDescriptor) {
        this.fileDescriptor = fileDescriptor;
    }

    private MediaErrorListener mErrorListener;
    private MediaPreparedListener mMediaPreparedListener;
    private AudioInfoListener mAudioInfoListener;
    private VideoInfoListener mVideoInfoListener;

    public void setOnVideoInfoListener(VideoInfoListener listener) {
        mVideoInfoListener = listener;
    }

    private void onVideoInfoListener(int width, int height) {
        if (mVideoInfoListener != null) {
            mVideoInfoListener.onSizeInfo(width, height);
        }
    }

    public void setOnErrorListener(MediaErrorListener errorListener) {
        this.mErrorListener = errorListener;
    }

    private void onError(int code, String msg) {
        if (mErrorListener != null) {
            mErrorListener.onError(code, msg);
        }
    }

    public void setOnPreparedListener(MediaPreparedListener preparedListener) {
        this.mMediaPreparedListener = preparedListener;
    }

    private void onPrepared() {
        if (mMediaPreparedListener != null) {
            mMediaPreparedListener.onPrepare();
        }
    }

    public void setOnAudioInfoListener(AudioInfoListener audioInfoListener) {
        mAudioInfoListener = audioInfoListener;
    }

    private void onProgress() {
    }

    private void onComplete() {

    }

    public void setSurface(Surface surface) {
        this.mSurface = surface;
        nSetSurface(surface);
    }

    public void prepare() {
        if (TextUtils.isEmpty(url) && fileDescriptor == null) {
            throw new NullPointerException("url is null");
        }
        if (fileDescriptor != null) {
            nPrepareFd(fileDescriptor, mAudioInfoListener);
        } else if (url != null) {
            nPrepare(url, mAudioInfoListener);
        }
    }

    public void prepareAsync() {
        if (TextUtils.isEmpty(url) && fileDescriptor == null) {
            throw new NullPointerException("url is null");
        }
        if (fileDescriptor != null) {
            nPrepareAsyncFd(fileDescriptor, mAudioInfoListener);
        } else if (url != null) {
            nPrepareAsync(url, mAudioInfoListener);
        }
    }

    public void play() {
        nPlay();
    }

    public void pause() {

    }

    public void stop() {
        nStop();
    }

    private native void nInit();

    private native void nPrepare(String url_, AudioInfoListener listener);

    private native void nPrepareAsync(String url_, AudioInfoListener listener);

    private native void nPrepareFd(FileDescriptor fileDescriptor, AudioInfoListener listener);

    private native void nPrepareAsyncFd(FileDescriptor fileDescriptor, AudioInfoListener listener);

    private native void nSetSurface(Surface surface);

    private native void nSeek(int second);

    private native void nPlay();

    private native void nStop();

    private native void setNativeAudioInfoListener(AudioInfoListener audioInfoListener);
}
